package com.somiz.admin;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import org.hibernate.Session;

import com.gest.util.AESencrp;
import com.somiz.main.Control;
import com.tech.hibernate.HibernateUtil;

public class AdminForm extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3898769532171909046L;
	private JTextField T_admin;
	private JPasswordField T_mdp;
	private JComboBox C_niveau;
	private static Long idAdmin;

	/**
	 * Create the panel.
	 */
	public AdminForm() {
		setLayout(null);

		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Nouveau compte :",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 101, 370, 148);
		add(panel);

		JLabel label = new JLabel("Nom utilisateur :");
		label.setBounds(29, 26, 101, 14);
		panel.add(label);

		JLabel label_1 = new JLabel("Mot de pass :");
		label_1.setBounds(29, 65, 101, 14);
		panel.add(label_1);

		T_admin = new JTextField();
		T_admin.setColumns(10);
		T_admin.setBounds(129, 23, 220, 34);
		panel.add(T_admin);

		T_mdp = new JPasswordField();
		T_mdp.setBounds(129, 62, 220, 34);
		panel.add(T_mdp);

		C_niveau = new JComboBox();
		C_niveau.addItem("Administrateur");
		C_niveau.addItem("Consultant");
		C_niveau.setBounds(129, 103, 220, 34);
		panel.add(C_niveau);

		JLabel label_2 = new JLabel("Droit :");
		label_2.setBounds(29, 106, 101, 14);
		panel.add(label_2);

		JButton B_save = new JButton("");
		B_save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Session session = HibernateUtil.getSessionFactory()
						.openSession();
				if (errForm().equals("")) {
					Admin admin = new Admin();
					admin = Control.getAdminDao().TrouverAvecId(idAdmin, admin,
							session);
					try {
						admin = FormVersAdmin(admin);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Control.getAdminDao().modifier(admin, session);
					session.close();
				} else {
					JOptionPane.showMessageDialog(null, errForm(), "Erreur",
							JOptionPane.WARNING_MESSAGE);
				}
			}
		});
		B_save.setIcon(new ImageIcon(AdminForm.class
				.getResource("/cc/black/png/save_icon&32.png")));
		B_save.setPreferredSize(new Dimension(50, 50));
		B_save.setMnemonic(KeyEvent.VK_ENTER);
		B_save.setBackground(Color.WHITE);
		B_save.setBounds(10, 11, 50, 50);
		add(B_save);

		JSeparator separator = new JSeparator();
		separator.setBounds(10, 81, 370, 2);
		add(separator);
	}

	public void AdminVersForm(Admin admin) {
		T_admin.setText(admin.getNom());
		try {
			T_mdp.setText(AESencrp.decrypt(admin.getMdp()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		C_niveau.setSelectedItem(admin.getNiveau());
	}

	public Admin FormVersAdmin(Admin admin) throws Exception {
		admin.setNom(T_admin.getText());
		admin.setMdp(AESencrp.encrypt(T_mdp.getText()));
		admin.setNiveau(C_niveau.getSelectedItem().toString());
		return admin;
	}

	public String errForm() {
		String err = "";
		if (T_admin.toString().equals("")) {
			err = err + "- Il faut choisire un utilisateur\n";
		}
		if (T_mdp.getText().length() <= 4) {
			err = err
					+ "- Le mot de pass doit contenire au moin 4 caracteres\n";
		}
		if (C_niveau.getSelectedItem().toString().equals("")) {
			err = err
					+ "- il faut choisire un niveau pour le nouveau utilisateur\n";
		}
		return err;
	}

	public void vider() {
		T_admin.setText("");
		T_mdp.setText("");
		C_niveau.setSelectedIndex(0);
	}

	public static Long getIdAdmin() {
		return idAdmin;
	}

	public static void setIdAdmin(Long idAdmin) {
		AdminForm.idAdmin = idAdmin;
	}
}
